using System.Windows.Forms;

namespace 第五次作业
{
    public partial class Form1 : Form
    {
        private Graphics graphics;
        private int depth = 7;
        private double leng = 100;
        private double per1 = 0.6;
        private double per2 = 0.7;
        private double th1 = 30 * Math.PI / 180;
        private double th2 = 45 * Math.PI / 180;
        private Color penColor = Color.Red;

        public Form1()
        {
            InitializeComponent();
            graphics = panel1.CreateGraphics();
        }

        private void buttonDraw_Click(object sender, EventArgs e)
        {
            graphics.Clear(panel1.BackColor);
            DrawCayleyTree(depth, panel1.Width / 2.0, panel1.Height - 20, leng, -Math.PI / 2, penColor);
        }

        private void DrawCayleyTree(int depth, double x0, double y0, double leng, double th, Color penColor)
        {
            if (depth == 0)
            {
                return;
            }

            double x1 = x0 + leng * Math.Cos(th);
            double y1 = y0 + leng * Math.Sin(th);

            Pen pen = new Pen(penColor);
            graphics.DrawLine(pen, (float)x0, (float)y0, (float)x1, (float)y1);

            double th11 = th + th1;
            double th22 = th - th2;

            DrawCayleyTree(depth - 1, x1, y1, per1 * leng, th11, penColor);
            DrawCayleyTree(depth - 1, x1, y1, per2 * leng, th22, penColor);
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            depth = (int)numericUpDown1.Value;
        }

        private void numericUpDown2_ValueChanged(object sender, EventArgs e)
        {
            leng = (double)numericUpDown2.Value;
        }

        private void numericUpDown3_ValueChanged(object sender, EventArgs e)
        {
            per1 = (double)numericUpDown3.Value;
        }

        private void numericUpDown4_ValueChanged(object sender, EventArgs e)
        {
            per2 = (double)numericUpDown4.Value;
        }

        private void numericUpDown5_ValueChanged(object sender, EventArgs e)
        {
            th1 = (double)numericUpDown5.Value * Math.PI / 180;
        }

        private void numericUpDown6_ValueChanged(object sender, EventArgs e)
        {
            th2 = (double)numericUpDown6.Value * Math.PI / 180;
        }

        private void buttonPenColor_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                penColor = colorDialog1.Color;
                buttonPenColor.BackColor = penColor;
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
    }
}